$(function () {
    getValue()
    //个人中心的展示编辑数据事件
    function getValue() {
        $.ajax({
            type: 'get',
            url: News.user_detail,
            headers: { Authorization: localStorage.getItem('get_big_token') },
            success: function (res) {
                $('.form-group').each(function (index, value) {
                    let name = $(value).find('input').prop('name')
                    if (index == 3) {
                        $(this).find('img').prop('src', res.data[name])
                    } else {
                        $(value).find('input').val(res.data[name])
                    }
                })
            },
            dataType: 'json'
        })
    }


    //上传图片的改变事件处理
    $('#exampleInputFile').on('change', function () {
        if ($('#exampleInputFile')[0].files[0].type.indexOf('image') == -1) {
            alert('请上传图片作为封面')
            return
        }
        let file = $(this)[0].files[0]
        let url = URL.createObjectURL(file);
        $('.user_pic').attr('src', url);
    })

    //修改按钮的点击事件
    $('.btn-edit').on('click', function () {
        let form = $('#form')[0]
        let formdata = new FormData(form)
        let Upimgs = $('#exampleInputFile')[0].files[0]
        formdata.set('userPic', Upimgs)
        $.ajax({
            type: 'post',
            url: News.user_edit,
            data: formdata,
            headers: { Authorization: localStorage.getItem('get_big_token') },
            processData: false,
            contentType: false,
            success: function (res) {
                alert(res.msg)
                window.parent.location.reload()
                getValue()
            },
            dataType: 'json'
        })

    })
})